package com.zhang.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhang.entity.ModelDictionary;
import com.zhang.entity.OperationLog;
import com.zhang.entity.UserInfo;
import com.zhang.entity.dto.GetLogDto;
import com.zhang.entity.dto.MyAuthorityRecord;
import com.zhang.entity.dto.UpdateUserDto;
import com.zhang.entity.vo.*;
import com.zhang.exception.ServiceException;
import com.zhang.mapper.AuthorityRecordMapper;
import com.zhang.mapper.ModelDictionaryMapper;
import com.zhang.mapper.OperationLogMapper;
import com.zhang.mapper.UserMapper;
import com.zhang.util.TimeUtil;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import static com.zhang.common.Constant.*;

/**
 * User: 张
 * Date: 2024-10-31
 * Time: 10:55
 * Description:
 */
@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ModelDictionaryMapper dictionaryMapper;
    @Autowired
    private AuthorityRecordMapper authorityRecordMapper;
    @Autowired
    private OperationLogMapper operationLogMapper;
    @Autowired
    private TimeUtil timeUtil;

    public PageInfo<UserInfo> queryUser(QueryUserBody queryUserBody, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<UserInfo> userInfos = userMapper.queryUser(queryUserBody.getUserName(),
                queryUserBody.getPhone(),
                queryUserBody.getEmail(),
                (queryUserBody.getUserState() == null) ? -1 : queryUserBody.getUserState());
        //  删除密码
        for (UserInfo userInfo : userInfos) userInfo.setPassword(null);
        return new PageInfo<>(userInfos);
    }

    public Boolean deleteUser(DeleteUserBody deleteUserBody){
        return userMapper.updateUserState(deleteUserBody.getUserId(),deleteUserBody.getUserState()) > ZERO;
    }

    public Boolean updateUser(UpdateUserBody updateUserBody){
        if (updateUserBody.getUserId() == null) throw new ServiceException(USER_ID_NOT_EMPTY);
        return userMapper.updateUser(updateUserBody.getUserId(),
                updateUserBody.getUserName(),
                updateUserBody.getPhone(),
                updateUserBody.getImageId(),
                updateUserBody.getEmail()
        ) > 0;
    }

    public PageInfo<ModelDictionary> getAuthority(Integer pageSize,Integer pageNum){
        PageHelper.startPage(pageNum, pageSize);
        List<ModelDictionary> dictionaryList = dictionaryMapper.getAuthority();
        return new PageInfo<>(dictionaryList);
    }

    public Boolean updateAuthorityRecord(UpdateAuthorityRecordBody updateAuthorityRecordBody){
        int removeResult = authorityRecordMapper.removeModelList(
                updateAuthorityRecordBody.getUserId(),
                updateAuthorityRecordBody.getRemoveModelId()
        );
        int addResult = 0;
        for (Integer modelId : updateAuthorityRecordBody.getAddModelId()) {
            addResult += authorityRecordMapper.addModelList(
                    updateAuthorityRecordBody.getUserId(),
                    modelId
            );
        }
        return removeResult > ZERO || addResult > ZERO;
    }

    public PageInfo<OperationLog> getLog(GetLogBody getLogBody, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        //  对时间格式进行校验
        GetLogDto getLogDto = new GetLogDto(getLogBody.getBusinessName(), timeUtil.checkTimeFormat(getLogBody.getTime()));
        List<OperationLog> operationLogs = operationLogMapper.
                getAllLog(
                        getLogDto.getBusinessName(),
                        getLogDto.getTime()
                );
        return new PageInfo<>(operationLogs);
    }

    public List<MyAuthorityRecord> getMyAuthority(Integer userId){
        return authorityRecordMapper.getMyAuthorityRecord(userId);
    }

    public List<UserInfo> getMyMessage(Integer userId){
        List<UserInfo> userInfos = userMapper.queryUserById(userId);
        for (UserInfo userInfo : userInfos) userInfo.setPassword(null);
        return userInfos;
    }


}
















